package com.yudongyang.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.yudongyang.domain.Disease;
import com.yudongyang.domain.Patient;
import com.yudongyang.service.PatientService;

@Controller
public class PatientController {

	@Resource
	private PatientService patientService;
	
	/**
	 * 
	 * @Title: list 
	 * @Description: 展示页面
	 * @param model
	 * @param p
	 * @param pageNum
	 * @param pageSize
	 * @return
	 * @return: String
	 */
	@RequestMapping("/list")
	public String list(Model model,Patient p,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize) {
		PageInfo<Patient> info = patientService.list(p,pageNum,pageSize);
		model.addAttribute("info", info);
		model.addAttribute("p", p);
		return "list";
	}
	/**
	 * 
	 * @Title: add 
	 * @Description: 去添加
	 * @return
	 * @return: String
	 */
	@GetMapping("/add")
	public String add() {
		
		return "add";
	}
	/**
	 * 
	 * @Title: getDisease 
	 * @Description: 获得所有的疾病
	 * @param model
	 * @return
	 * @return: List<Disease>
	 */
	@ResponseBody
	@PostMapping("/getDisease")
	public List<Disease> getDisease(Model model) {
		List<Disease> list = patientService.getDisease();
		
		
		return list;
	}
	/**
	 * 
	 * @Title: add 
	 * @Description: 添加
	 * @param p
	 * @return
	 * @return: boolean
	 */
	@ResponseBody
	@PostMapping("/add")
	public boolean add(Patient p) {
		int i = patientService.add(p);
		return i>0;
	}
	/**
	 * 
	 * @Title: getOne 
	 * @Description: 获取一个回显
	 * @param id
	 * @param model
	 * @return
	 * @return: String
	 */
	@GetMapping("/getOne")
	public String getOne(String id,Model model) {
		Patient p = patientService.getOne(id);
		System.out.println(p);
		model.addAttribute("p", p);
		return "update";
	}
	/**
	 * 
	 * @Title: update 
	 * @Description: 修改
	 * @param p
	 * @return
	 * @return: boolean
	 */
	@ResponseBody
	@PostMapping("/update")
	public boolean update(Patient p) {
		int i = patientService.update(p);
		return i>0;
	}
}
